//
//  KeleMainController.m
//  KeleAppProject
//
//  Created by Cen Zhou on 2018/1/10.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "KeleMainController.h"
#import "KeleNavigationController.h"
#import "KeleTabBar.h"
#import "HomeViewController.h"
#import "CategoryViewController.h"
#import "SortViewController.h"
#import "MineViewController.h"
#import "RecordViewController.h"

@interface KeleMainController ()<UITabBarControllerDelegate>
@property (nonatomic,assign) NSInteger  indexFlag;　　//记录上一次点击tabbar，使用时，记得先在init或viewDidLoad里 初始化 = 0

@end

@implementation KeleMainController

+ (instancetype)sharedMainViewController
{
    
    static KeleMainController * mainTabbar = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mainTabbar = [[[self class] alloc] init];
    });
    return mainTabbar;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupItemTitleTextAttributes];
    [self setupTabBar];
    [self addAllChildVcs];
}


- (void)setupTabBar
{
    KeleTabBar *keleTabbar = [[KeleTabBar alloc] init];
    [self setValue:keleTabbar forKeyPath:@"tabBar"];
    keleTabbar.delegateSignal = [RACSubject subject];
    [keleTabbar.delegateSignal subscribeNext:^(id x) {
        RecordViewController *recordVc = [[RecordViewController alloc]init];
        KeleNavigationController *nav = [[KeleNavigationController alloc]initWithRootViewController:recordVc];
        [self presentViewController:nav animated:YES completion:nil];
    }];
    
}

/**
 *  设置所有UITabBarItem的文字属性
 */
- (void)setupItemTitleTextAttributes
{
    UITabBarItem *item = [UITabBarItem appearance];
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:11];
    normalAttrs[NSForegroundColorAttributeName] = KLColor_A3;
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:12];
    selectedAttrs[NSForegroundColorAttributeName] = KLColor_A1;
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}

/**
 *  添加所有的子控制器
 */
- (void)addAllChildVcs
{
    HomeViewController *homeVC = [[HomeViewController alloc] init];
    [self addOneChildVC:[[KeleNavigationController alloc] initWithRootViewController:homeVC] title:@"首页" image:@"tabbar_icon_home_nor" selectedImage:@"tabbar_icon_home_pre"];
    
    SortViewController *sortVC = [[SortViewController alloc] init];
    [self addOneChildVC:[[KeleNavigationController alloc] initWithRootViewController:sortVC] title:@"排行" image:@"tabbar_icon_paihang_nor" selectedImage:@"tabbar_icon_paihang_pre"];
    
    CategoryViewController *categoryVC = [[CategoryViewController alloc]init];
    [self addOneChildVC:[[KeleNavigationController alloc]initWithRootViewController:categoryVC] title:@"分类" image:@"tabbar_icon_fenlei_nor" selectedImage:@"tabbar_icon_fenlei_pre"];
    
    MineViewController *mineVC = [[MineViewController alloc] init];
    [self addOneChildVC:[[KeleNavigationController alloc] initWithRootViewController:mineVC] title:@"我的" image:@"tabbar_icon_wode_nor" selectedImage:@"tabbar_icon_wode_pre"];
    
    
}

/**
 *  添加一个子控制器
 *
 *  @param childVC       子控制器对象
 *  @param title         标题
 *  @param image         图标
 *  @param selectedImage 选中的图标
 */
- (void)addOneChildVC:(UIViewController *)childVC title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    childVC.tabBarItem.title = title;
    
    childVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    // 添加为tabbar控制器的子控制器
    [self addChildViewController:childVC];
}


-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index != self.indexFlag) {
        //执行动画
        NSMutableArray *arry = [NSMutableArray array];
        for (UIView *btn in self.tabBar.subviews) {
            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [arry addObject:btn];
            }
        }
        //添加动画
        //放大效果，并回到原位
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        //速度控制函数，控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.1;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
        animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
        self.indexFlag = index;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
